<template>
  <div class="todo-footer" v-show="total">
    <label>
      <!-- <input type="checkbox" :checked="isall" @change="checkall" /> -->
      <!-- 还可以直接采用v-model来绑定是否选中，但是这种写法需要isall计算属性的完整写法，设置setter -->
      <input type="checkbox" v-model="isall" />
    </label>
    <span>
      <span>已完成{{ isdone }}</span> / 全部{{ total }}
    </span>
    <button class="btn btn-danger" @click="clearall">清除已完成任务</button>
  </div>
</template>

<script>
export default {
  name: "MyFooter",
  data() {
    return {};
  },
  methods: {
    /* checkall(e) {
      this.checkalltodo(e.target.checked);
    }, */
    clearall() {
      this.clearalltodo();
    },
  },
  computed: {
    isdone() {
      /* let i = 0;
      this.todos.forEach((todo) => {
        if (todo.done) i++;
      });
      return i; */

      //用es6的 reduce函数
      /* const x = this.todos.reduce((pre, current) => {
        console.log("@", pre, current);
        return pre + (current.done ? 1 : 0);
      }, 0);
      return x; */

      return this.todos.reduce((pre, todo) => pre + (todo.done ? 1 : 0), 0);
    },
    total() {
      return this.todos.length;
    },
    // isall() {
    //   return this.isdone === this.total && this.total > 0;
    // },
    isall: {
      get() {
        return this.isdone === this.total && this.total > 0;
      },
      set(value) {
        this.checkalltodo(value);
      },
    },
  },
  props: ["todos", "checkalltodo", "clearalltodo"],
  components: {},
};
</script>

<style scoped lang="css">
/*footer*/
.todo-footer {
  height: 40px;
  line-height: 40px;
  padding-left: 6px;
  margin-top: 5px;
}

.todo-footer label {
  display: inline-block;
  margin-right: 20px;
  cursor: pointer;
}

.todo-footer label input {
  position: relative;
  top: -1px;
  vertical-align: middle;
  margin-right: 5px;
}

.todo-footer button {
  float: right;
  margin-top: 5px;
}
</style>
